package com.xunqi.ad.controller;

import com.alibaba.fastjson.JSON;
import com.xunqi.ad.annotation.IgnoreResponseAdvice;
import com.xunqi.ad.client.SponsorClient;
import com.xunqi.ad.client.form.AdPlanGetForm;
import com.xunqi.ad.client.vo.AdPlanVo;
import com.xunqi.ad.vo.ResponseVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * @Description:
 * @Created with IntelliJ IDEA.
 * @author: 夏沫止水
 * @createTime: 2020-05-02 19:52
 **/
@Slf4j
@RestController
public class SearchController {

    @Resource
    private RestTemplate restTemplate;

    @Resource
    private SponsorClient sponsorClient;

    @IgnoreResponseAdvice
    @PostMapping(value = "/getAdPlansByFeign")
    public ResponseVo<List<AdPlanVo>> getAdPlansByFeign(@Valid @RequestBody AdPlanGetForm form) {

        log.info("ad-search:getAdPlansByFeign -> {}", JSON.toJSONString(form));

        ResponseVo<List<AdPlanVo>> adPlansByRibbon = sponsorClient.getAdPlansByRibbon(form);

        return adPlansByRibbon;

    }


    @SuppressWarnings("all")
    @IgnoreResponseAdvice
    @PostMapping(value = "/getAdPlansByRibbon")
    public ResponseVo<List<AdPlanVo>> getAdPlansByRibbon(@Valid @RequestBody AdPlanGetForm form) {

        log.info("ad-search:getAdPlansByRibbon -> {}", JSON.toJSONString(form));

        ResponseVo bodyResponseVo = restTemplate.postForEntity(
                "http://eureka-client-ad-sponsor/ad-sponsor/get/adPlan",
                form,
                ResponseVo.class).getBody();

        // ResponseVo forObject = restTemplate.getForObject(
        //         "http://eureka-client-ad-sponsor/ad-sponsor/get/adPlan",
        //         ResponseVo.class,
        //         form);

        return bodyResponseVo;

    }
}
